package com.haierCamera.customapplication.ui.plan;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.ui.business.Business;
import com.haierCamera.customapplication.ui.business.entity.ChannelInfo;
import com.lechange.opensdk.api.bean.DeviceAlarmPlan;

import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2018/6/20.
 */

public class HZKLMovePlanTimeActivity extends BaseActivity {
    private ListView listView;
    private ChannelInfo channelInfo;
    private MyAdapter adapter;

    private String deviceId;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hzkl_activity_plan_time);
        initTitle();
        initData();
    }

    /**
     * 初始化标题栏
     */
    public void initTitle() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void initData() {
        listView = (ListView) findViewById(R.id.listView);
        deviceId = getIntent().getStringExtra("deviceId");
        channelInfo = Business.getInstance().getChannel(deviceId);
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("请稍等...");
        listView.setOnItemClickListener((parent, view, position, id) -> startActivity(new Intent(HZKLMovePlanTimeActivity.this, HZKLPlanTimeSettingActivity.class).putExtra("deviceId", deviceId)));
    }

    @Override
    protected void onResume() {
        super.onResume();
        //getAlarmPlan();
    }

    @SuppressLint("HandlerLeak")
    private void getAlarmPlan() {
        week = "";
        progressDialog.show();
        Business.getInstance().getAlarmPlanConfig(channelInfo.getDeviceCode(), String.valueOf(channelInfo.getIndex()), new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0) {
                    progressDialog.dismiss();
                    DeviceAlarmPlan.Response resp = (DeviceAlarmPlan.Response) (((Business.RetObject) msg.obj).resp);
                    List<DeviceAlarmPlan.ResponseData.RulesElement> rulesElements = resp.data.rules;
                    adapter.upDate(rulesElements);
                } else {
                    progressDialog.dismiss();
                    Toast.makeText(HZKLMovePlanTimeActivity.this, ((Business.RetObject) msg.obj).mMsg, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    String week = "";

    class MyAdapter extends BaseAdapter {
        private List<DeviceAlarmPlan.ResponseData.RulesElement> rulesElements;

        public void upDate(List<DeviceAlarmPlan.ResponseData.RulesElement> rulesElement) {
            this.rulesElements = rulesElement;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return null == rulesElements ? 0 : rulesElements.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(HZKLMovePlanTimeActivity.this, R.layout.hzkl_activity_plan_item, null);
                holder = new ViewHolder();
                holder.plan = (LinearLayout) convertView.findViewById(R.id.plan_ll);
                holder.left = (TextView) convertView.findViewById(R.id.plan_left);
                holder.right = (TextView) convertView.findViewById(R.id.plan_right);
                holder.day = (TextView) convertView.findViewById(R.id.plan_day);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.left.setText(rulesElements.get(position).beginTime + "~");
            holder.right.setText(rulesElements.get(position).endTime);
            String period = rulesElements.get(position).period;
            if ("everyday".equals(period)) {
                week = "每天";
            } else if ("workday".equals(period)) {
                week = "工作日";
            } else {
                if (period.contains("Monday")) {
                    week = "每周一";
                }
                if (period.contains("Tuesday")) {
                    week += "每周二";
                }
                if (period.contains("Wednesday")) {
                    week += "每周三";
                }
                if (period.contains("Thursday")) {
                    week += "每周四";
                }
                if (period.contains("Friday")) {
                    week += "每周五";
                }
                if (period.contains("Saturday")) {
                    week += "每周六";
                }
                if (period.equals("Sunday")) {
                    week += "每周日";
                }
            }
            holder.day.setText(week);
            return convertView;
        }
    }

    class ViewHolder {
        LinearLayout plan;
        TextView left, right, day;
    }
}
